<template>
  <div>
    <a-form ref="queryFormRef" name="queryForm" :model="queryForm.data" :labelCol="{ span: 8 }" :wrapperCol="{ span: 14 }">
      <a-row :gutter="24">
        <a-col span="8">
          <a-form-item name="tableName" label="表名称">
            <a-input v-model:value="queryForm.data.tableName"></a-input>
          </a-form-item>
        </a-col>
        <a-col span="8">
          <a-form-item name="tableComment" label="表描述">
            <a-input v-model:value="queryForm.data.tableComment"></a-input>
          </a-form-item>
        </a-col>

        <a-col :span="8">
          <a-form-item name="beginTime" label="创建起期">
            <a-date-picker v-model:value="queryForm.data.params.beginTime" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item name="endTime" label="创建止期">
            <a-date-picker v-model:value="queryForm.data.params.endTime" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24" class="autoee-button-row">
        <a-col :span="12" style="text-align: left">
          <a-button type="primary" @click="openImportTablesModal()"><AutoeeIcon icon="plus-outlined" />导入表</a-button>
          <a-button type="primary" danger @click="clickDelete()" :disabled="tableData.selectedRowKeys.length <= 0"><AutoeeIcon icon="delete-outlined" />删除</a-button>
        </a-col>
        <a-col :span="12" style="text-align: right">
          <a-button type="primary" @click="tablePage.reload()"> <AutoeeIcon icon="SearchOutlined" />查询 </a-button>
          <a-button @click="resetQueryForm()"><AutoeeIcon icon="redo-outlined" />重置</a-button>
        </a-col>
      </a-row>
    </a-form>
    <!-- tableLayout设为 fixed 表示内容不会影响列的布局 	- | 'auto' | 'fixed' -->
    <a-table
      rowKey="tableId"
      :columns="tableData.columns"
      :data-source="tableData.dataSource"
      :loading="tablePage.loading.value"
      :pagination="tablePagination"
      @change="tableHandChangePage"
      :row-selection="{ selectedRowKeys: tableData.selectedRowKeys, onChange: onTableSelectChange }"
      bordered
      size="small"
      tableLayout="fixed"
    >
      <template #bodyCell="{ index, column, record }">
        <template v-if="column.key === 'pageIndex'">
          {{ index + 1 }}
        </template>
        <template v-else-if="column.key === 'status'">
          <span>
            <a-tag v-if="record.status === '0'" color="green" style="width: 50px; text-align: center">
              {{ AutoeeUtil.getDictLabelByKey(record.status, pageData.statusDict) }}
            </a-tag>
            <a-tag v-else-if="record.status === '1'" color="red" style="width: 50px; text-align: center">
              {{ AutoeeUtil.getDictLabelByKey(record.status, pageData.statusDict) }}
            </a-tag>
          </span>
        </template>
        <template v-else-if="column.key === 'sex'">
          <span>
            {{ AutoeeUtil.getDictLabelByKey(record.sex, pageData.sexDict) }}
          </span>
        </template>
        <template v-else-if="column.key === 'operateCol'">
          <span>
            <!-- <a @click="openUpdateModal(record)"> 预览 </a>
            <a-divider type="vertical" /> -->
            <a @click="openUpdateModal(record)"> 修改 </a>
            <a-divider type="vertical" />
            <!-- <a @click="openUpdateModal(record)"> 同步 </a>
            <a-divider type="vertical" /> -->
            <a @click="generateCode(record)"> 生成代码 </a>
          </span>
        </template>
      </template>
    </a-table>

    <GenCodeConfigUpdate ref="genCodeConfigUpdateRef" />
    <ImportTables ref="importTablesRef" @tableReload="tableReload" />
  </div>
</template>

<script setup>
import { listTable, previewTable, delTable, genCode, synchDb, batchGenCode } from '@/api/tool/gen';
import GenCodeConfigUpdate from './genCodeConfigUpdate.vue';
import ImportTables from './importTables.vue';
import { computed, reactive, ref } from 'vue';
import AutoeeUtil from '@/utils/AutoeeUtil.js';
import { AutoeeIcon } from '@/utils/AutoeeIcon.js';
import { message } from 'ant-design-vue';

// 当前页面使用的数据
const pageData = reactive({
  sexDict: [],
  deptTreeData: [],
  statusDict: [],
  postDict: [],
  roleDict: [],
});
AutoeeUtil.getDictsByType('sys_user_sex').then((res) => {
  pageData.sexDict = res;
});
AutoeeUtil.getDeptTreeData().then((res) => {
  pageData.deptTreeData = res.data;
});
AutoeeUtil.getDictsByType('sys_normal_disable').then((res) => {
  pageData.statusDict = res;
});

// 查询Form
const queryFormRef = ref();
const queryForm = reactive({ data: { params: {} } });

// 重置查询Form
const resetQueryForm = () => {
  AutoeeUtil.resetFormFieldsToEmpty(queryForm.data);
  tablePage.reload();
};
const tableReload = () => {
  tablePage.reload();
};

// 用户列表数据
const tableData = reactive({
  // 列表数据
  dataSource: [],
  // 列表记录数
  total: 0,
  selectedRowKeys: [],
  // 列表列定义
  columns: [
    {
      title: '序号',
      dataIndex: 'pageIndex',
      key: 'pageIndex',
      width: 50,
      align: 'center',
    },
    {
      title: '表名称',
      dataIndex: 'tableName',
      key: 'tableName',
    },
    {
      title: '表描述',
      dataIndex: 'tableComment',
      key: 'tableComment',
    },
    {
      title: '实体',
      dataIndex: 'className',
      key: 'className',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      key: 'updateTime',
    },
    {
      title: '操作',
      key: 'operateCol',
      width: 180,
    },
  ],
});

// 列表选中方法
const onTableSelectChange = (selectedRowKeys) => {
  tableData.selectedRowKeys = selectedRowKeys;
};

// 查询列表
const queryDataList = () => {
  return listTable(queryForm.data);
};

// 用户列表翻页数据：使用vue-request中分页方法，返回参数包含{ run, reload, loading, current, pageSize }
const tablePage = AutoeeUtil.usePagination(queryDataList, tableData);
console.log('tablePage=' + JSON.stringify(tablePage));

// 用户列表翻页工具条：必须通过计算函数每次重新生成
const tablePagination = computed(() => AutoeeUtil.getTablePagination(tablePage));

// 手动翻页方法
const tableHandChangePage = (page, filters, sorter) => {
  AutoeeUtil.tableHandChangePage(tablePage, page, filters, sorter);
};

// 打开导入表窗口
const importTablesRef = ref();
const openImportTablesModal = () => {
  importTablesRef.value.openModal(tablePage);
};

// 打开修改窗口
const genCodeConfigUpdateRef = ref();
const openUpdateModal = (record) => {
  genCodeConfigUpdateRef.value.openUpdateModal(record, tablePage);
};

const userDetailRef = ref();
// 打开详细窗口
const clickUserDetail = (record) => {
  userDetailRef.value.openModal(record);
};

// 生成代码
const uploadRef = ref();
const generateCode = (record) => {
  const tableNames = record.tableName;

  if (record.genType === '1') {
    genCode(record.tableName).then((response) => {
      AutoeeUtil.messageSuccess('成功生成到自定义路径：' + record.genPath);
    });
  } else {
    AutoeeUtil.zip('/tool/gen/batchGenCode?tables=' + tableNames, '表[' + tableNames + ']代码文件.zip');
  }
};

// 点击删除
const clickDelete = () => {
  AutoeeUtil.confirmDeleteSelectedData(() => {
    delTable(tableData.selectedRowKeys).then((res) => {
      AutoeeUtil.messageSuccess('删除操作成功。');
      tablePage.reload();
    });
  });
};
</script>

<style lang="less"></style>
